layui.define(['layer', 'jquery'], function (exports) {
    "use strict";

    var $ = layui.$,
        layer = layui.layer;


    var request = function (opt) {
        var defaultSettings = {
            data: {},
            type: 'get',
            dataType: 'json',
            headers: {'Authorization': layui.data('userInfo').token},
            beforeSend: function () {
                // openLoad('loading.gif')
            },
            complete: function () {
                // closeLoad();
            },
            // error: function (xhr) {
            //     layer.alert("系统异常~");
            //     window.location.href = 'login.html';
            //     window.localStorage.clear();
            // }
        };
        for (let key in opt) {
            defaultSettings[key] = opt[key];
            if (key === 'url')
                defaultSettings[key] = '/yuan/' + opt[key];
        }
        if (arguments[1]) { //如果是图片上传,需要传递第二个参数，必须内容
            defaultSettings.traditional = true;
            defaultSettings.processData = false;
            defaultSettings.contentType = false;
        }
        return new Promise((resolve, reject) => {
            $.ajax(defaultSettings).then(data => {
                if (data.code === '200') {
                    resolve(data.data);
                } else {
                    reject(data.message);
                }
            },e => {
                reject('系统异常了');
            });
        });
    };


    function showTips(msg) {
        var tipsEl = $('<div></div>').text(msg).css({
            "text-align": "center",
            "line-height": "40px",
            "height": '40px',
            "background": 'rgba(0,0,0,0.5)',
            'position': 'fixed',
            "left": '50%',
            "top": '50%',
            "transform": 'translate(-50%,-50%)',
            "z-index": 999999,
            'padding': '15px 30px',
            "box-sizing": 'border-box',
            "display": 'none',
            "font-size": '14px'
        });

        tipsEl.appendTo($('body')).fadeIn(500).delay(1000).fadeOut(500, function () {
            tipsEl.remove()
        })
    }

    function getStore(name) {
        if (!name) return;
        return window.localStorage.getItem(name);
    }

    function openLoad(src) {
        var loadEl_icon = $('<i></i>').css({
            "width": '50px',
            "height": '50px',
            "margin": 'auto',
            "background": 'url(' + src + ') no-repeat center center',
            'background-size': 'cover'
        })
        var lodeEl_inner = $('<div class="ui-loading-inner"></div>').css({
            "width": '200px',
            "height": '200px',
            "margin": 'auto',
            "border-radius": '5px',
            "background": 'rgba(0,0,0,0.5)',
            "display": 'flex'
        }).append(loadEl_icon)
        var loadEl_wrap = $('<div class="ui-loading-mask"></div>').css({
            "position": 'absolute',
            "left": 0, "top": 0, "bottom": 0, "right": 0,
            "background": 'rgba(255,255,255,0.01)',
            "z-index": 999999,
            "display": 'flex'
        }).append(lodeEl_inner).appendTo($('body'))
    }

    function closeLoad() {
        $('.ui-loading-mask').remove()
    }

    exports("request", request);
});
